// pages/directDetail/directDetail.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [],
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    current: 0,
    activeColor: '#b3b3b3',
    indicatorColor: '#f2f1f1',
    circular: "true"
  },
  swichNav: function (e) {//点击切换样式
    var that = this;
    if (this.data.current === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        current: e.target.dataset.current
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    getBackDataHot(this, options.id)
    this.setData({
      imageUrl: app.globalData.imageUrl,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  gotoCarts:function(e){
   wx.navigateTo({
     url: '../../pages/directBuy/directBuy',
   })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (wx.getStorageSync('cartLists')) {//先判断购物车有无商品
      this.setData({
        cartList: wx.getStorageSync('cartLists')
      })
    } else {
      this.setData({
        cartList: []
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    return {
      title: '品德共享酒庄',
      path: '/pages/index/index?id=' + wx.getStorageSync("userid"),
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
    selectInfoDirect:function(){
    var that=this;
    wx.navigateTo({
      url: '../directInfo/directInfo?directId=' + that.data.unitProduct.directId,
    })
  }
  ,
  addToCart: function (e) {//加入购物车
    var that = this;
    console.log(that.data.cartList)
    if (that.data.cartList.length > 0) {
      var config = false;
      for (var x in that.data.cartList) {
        if (that.data.cartList[x].productId == e.target.dataset.id) {
          console.log()
          if (that.data.cartList[x].quantity == that.data.cartList[x].repertory) {//如果增加的数量大于库存，则取消增加；
            wx.showToast({
              title: "库存不足",
              icon: "loading"
            })
          } else {
            that.data.cartList[x].quantity += 1;
          }
          config = true;
        }
      }
      if (!config) {
        if (e.target.dataset.repertory == 0) {
          wx.showToast({
            title: "库存不足",
            icon: "loading"
          })
          return;
        }
        getCartInfo(that, e)//加入购物车
      }
    } else {
      if (e.target.dataset.repertory == 0) {
        wx.showToast({
          title: "库存不足",
          icon: "loading"
        })
        return;
      }
      getCartInfo(that, e)//加入购物车
    }
    wx.setStorageSync('cartLists', this.data.cartList)
   // console.log(wx.getStorageSync('cartLists'))
  },
  buy: function (e) {//立即购买
    var that = this;
    if (that.data.cartList.length > 0) {
      var config = false;
      for (var x in that.data.cartList) {
        if (that.data.cartList[x].productId == e.target.dataset.id) {
          if (that.data.cartList[x].quantity == that.data.cartList[x].repertory) {//如果增加的数量大于库存，则取消增加；
            wx.showToast({
              title: "库存不足",
              icon: "loading"
            })
          } else {
            that.data.cartList[x].quantity += 1;
          }
          config = true;
        }
      }
      
      if (!config) {
        if (e.target.dataset.repertory == 0) {
          wx.showToast({
            title: "库存不足",
            icon: "loading"
          })
          return;
        }
        getCartInfo(that, e)//加入购物车
      }
    } else {
      if (e.target.dataset.repertory==0){
        wx.showToast({
          title: "库存不足",
          icon: "loading"
        })
        return;
      }
      getCartInfo(that, e)//加入购物车
    }
    wx.setStorageSync('cartLists', this.data.cartList)
    console.log(wx.getStorageSync('cartLists'))
    wx.navigateTo({ 
      url: '../directBuy/directBuy',    
    })
  }
})

function getBackDataHot(that, id) {//获取单个商品的详情
  wx.request({
    url: app.globalData.allUrl + "/directAction/getDirectProduct",
    data: {
      id: id,
      directId: 1,
      pageNum: 1,
      pageCount: 30,
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
      console.log(res.data)
      if (res.data) {
        var imgUrls = [];
        res.data.list[0].sprice2 = parseFloat(res.data.list[0].price2 * 0.21).toFixed(2);
        res.data.list[0].sprice3 = parseFloat(res.data.list[0].price3 * 0.21).toFixed(2);
        res.data.list[0].sprice4 = parseFloat(res.data.list[0].price4 * 0.21).toFixed(2);
        res.data.list[0].sprice5 = parseFloat(res.data.list[0].price5 * 0.21).toFixed(2);
        res.data.list[0].sprice6 = parseFloat(res.data.list[0].price6 * 0.21).toFixed(2);
        if (wx.getStorageSync("userType") == 1) {
          res.data.list[0].rprice = res.data.list[0].price2
          res.data.list[0].resprice = res.data.list[0].sprice2
          res.data.list[0].realPrice = parseFloat(res.data.list[0].sprice2) + parseFloat(res.data.list[0].price2);
        } else if (wx.getStorageSync("userType") == 2) {
          res.data.list[0].rprice = res.data.list[0].price3
          res.data.list[0].resprice = res.data.list[0].sprice3
          res.data.list[0].realPrice = parseFloat(res.data.list[0].sprice3) + parseFloat(res.data.list[0].price3);
        } else if (wx.getStorageSync("userType") == 3) {
          res.data.list[0].rprice = res.data.list[0].price4
          res.data.list[0].resprice = res.data.list[0].sprice4
          res.data.list[0].realPrice = parseFloat(res.data.list[0].sprice4) + parseFloat(res.data.list[0].price4);
        } else if (wx.getStorageSync("userType") == 4) {
          res.data.list[0].rprice = res.data.list[0].price5
          res.data.list[0].resprice = res.data.list[0].sprice5
          res.data.list[0].realPrice = parseFloat(res.data.list[0].sprice5) + parseFloat(res.data.list[0].price5);
        } else if (wx.getStorageSync("userType") == 5) {
          res.data.list[0].rprice = res.data.list[0].price6
          res.data.list[0].resprice = res.data.list[0].sprice6
          res.data.list[0].realPrice = parseFloat(res.data.list[0].sprice6) + parseFloat(res.data.list[0].price6);
        }else{
          res.data.list[0].rprice = res.data.list[0].price2
          res.data.list[0].resprice = res.data.list[0].sprice2
          res.data.list[0].realPrice = parseFloat(res.data.list[0].sprice2) + parseFloat(res.data.list[0].price2);
        }       
        imgUrls.push({
          url: app.globalData.imageUrl + res.data.list[0].banner.picture1
        }, {
            url: app.globalData.imageUrl + res.data.list[0].banner.picture2
          }, {
            url: app.globalData.imageUrl + res.data.list[0].banner.picture3
          },
        )
        getwineTypes(that, res.data.list[0].classify)
        that.setData({
          imgUrls: imgUrls,
          unitProduct: res.data.list[0],
        //  Evaluates: res.data.list[0].Evaluates,
        //  DirectInfo: res.data.list[0].DirectInfo
        })
      }
    }
  })
}


function getCartInfo(that, e) {//加入购物车，将商品信息储存在当前页面
  var CartName = e.target.dataset.name;
  var CartPrice = e.target.dataset.price;
  var CartID = e.target.dataset.id;
  var CartSuper = e.target.dataset.storeName;
  var Cartquantity = 1;
  var totalPrices = "";
  var Cartimg = e.target.dataset.img;
 // var Cartdirectid = e.target.dataset.directid;
  var Cartrepertory = e.target.dataset.repertory;
  var integral = e.target.dataset.integral;
 
  var carts = { productName: CartName, unitPrice: CartPrice, productId: CartID, quantity: Cartquantity, totalPrices: totalPrices, repertory: Cartrepertory, img: Cartimg, directId: 1, integral: integral}
  that.data.cartList.push(carts);
  console.log(that.data.cartList)
}


 function getwineTypes(that,id){
   wx.request({
     url: app.globalData.allUrl + "/superMarketAction/getGoodsType2",
     data: {
       typeID:100
     },
     method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
     // header: {}, // 设置请求的 header
     success: function (res) {
      // console.log(res.data)
       for (var i=0;i<res.data.length;i++){
         if (res.data[i].id==id){
           that.setData({
             redWineType: res.data[i].goodsName
           })
         }
       }
     }
   })
 }